
package com.qianxun.init.config;


import com.qianxun.init.properties.SpringServerProperties;
import com.qianxun.init.properties.SysProperties;
import com.qianxun.module.datasource1.common.manager.CaptchaValidObjectHolder;
import com.qianxun.plugins.shiro.ShiroUtil;
import org.beetl.core.GroupTemplate;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


/**
 * @author huwei
 */
@Configuration
public class BeetlConfig {

    private final String templatesPath = "templates";

    private final Properties properties = new Properties();
    {
        //beetl 个性化配置
        properties.setProperty("DELIMITER_STATEMENT_START","@");
        properties.setProperty("DELIMITER_STATEMENT_END","");
    }

    @Resource
    private SpringServerProperties springServerProperties;

    @Resource
    private SysProperties sysProperties;

    /**
     * 创建共享变量
     */
    private Map<String ,Object> shareVar(){
        Map<String ,Object> map = new HashMap<>(1);
        map.put("contextPath" , springServerProperties.contextPath());
        map.put("systemName" ,sysProperties.getMaps().getOrDefault("system-name" ,"test-system"));
        return map;
    }

    /**
     * beet注册工具对象
     */
    private Map<String ,Object> registerFunction(){
        Map<String ,Object> map = new HashMap<>(1);
        map.put("ShiroUtil" ,new ShiroUtil());
        map.put("CaptchaValidObjectHolder" , CaptchaValidObjectHolder.getInstance());
        return map;
    }

    @Bean
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        //获取Spring Boot 的ClassLoader
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if(loader==null){
            loader = BeetlConfig.class.getClassLoader();
        }
        //设置个性化参数
        beetlGroupUtilConfiguration.setConfigProperties(properties);
        //设置共享变量
        beetlGroupUtilConfiguration.setSharedVars(shareVar());
        //html文件设置
        ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader, templatesPath);
        beetlGroupUtilConfiguration.setResourceLoader(cploder);

        beetlGroupUtilConfiguration.init();
        //如果使用了优化编译器，涉及到字节码操作，需要添加ClassLoader
        beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(loader);
        //注册全局工具
        GroupTemplate groupTemplate = beetlGroupUtilConfiguration.getGroupTemplate();
        registerFunction().forEach((k ,v)->{
            groupTemplate.registerFunctionPackage(k ,v);
        });
        return beetlGroupUtilConfiguration;

    }

    @Bean
    public BeetlSpringViewResolver getBeetlSpringViewResolver(BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }
}
